﻿namespace D.Domain.SystemDomain.SysAdminDomain
{
    /// <summary>
    /// 领域层服务
    /// </summary>
    public class SysAdminDomainService
    {
        private readonly ISmsCodeSneder smsCodeSneder;
        private readonly ISysUserRepository _sysUserRepository;

        public SysAdminDomainService(ISmsCodeSneder smsCodeSneder, ISysUserRepository sysUserRepository)
        {
            this.smsCodeSneder = smsCodeSneder;
            this._sysUserRepository = sysUserRepository;
        }

        #region 登录检查


        /// <summary>
        /// 检查登录
        /// </summary>
        /// <param name="phoneNumberVo">手机号</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        public async Task<EnumSysAdminLoginResult> CheckLoginAsync(PhoneNumberVo phoneNumberVo, string pwd)
        {
            EnumSysAdminLoginResult result;
            var sysAdminEntity = await _sysUserRepository.FindOneAsync(phoneNumberVo);
            if (sysAdminEntity == null)
            {// 找不到用户
                result = EnumSysAdminLoginResult.PhoneNumberNotFound;
            }
            else if (IsLockOut(sysAdminEntity))
            {// 用户被锁定
                result = EnumSysAdminLoginResult.Lockout;
            }
            else if (!sysAdminEntity.HasPasswprd())
            {// 没有设置密码
                result = EnumSysAdminLoginResult.NoPassword;
            }
            else if (sysAdminEntity.CheckPwd(pwd))
            {// 密码正确
                result = EnumSysAdminLoginResult.OK;
            }
            else
            {// 密码错误
                result = EnumSysAdminLoginResult.PasswordError;
            }

            if (sysAdminEntity != null)
            {
                if (result == EnumSysAdminLoginResult.OK)
                {
                    this.ResetLoginFail(sysAdminEntity);// 重置
                }
                else
                {
                    this.LoginFail(sysAdminEntity);// 处理登录失败
                }
            }
            await _sysUserRepository.PublishEventAsync(new SysUserLoginResultEvent(phoneNumberVo, result));
            return result;
        }

        public void ResetLoginFail(SysUserEntity sysAdminEntity)
        {
            sysAdminEntity.SysAdminLoginFail.Reset();
        }

        public bool IsLockOut(SysUserEntity sysAdminEntity)
        {
            return sysAdminEntity.SysAdminLoginFail.IsLockOut();
        }

        public void LoginFail(SysUserEntity sysAdminEntity)
        {
            sysAdminEntity.SysAdminLoginFail.Fail();
        }
        #endregion

        #region 验证码检查
        public async Task<EnumCheckCodeResult> CheckCodeAsync(PhoneNumberVo phoneNumberVo, string code)
        {
            var sysAdminEntity = await _sysUserRepository.FindOneAsync(phoneNumberVo);
            if (sysAdminEntity == null)
            {
                return EnumCheckCodeResult.PhoneNumberNotFound;
            }
            if (IsLockOut(sysAdminEntity))
            {
                return EnumCheckCodeResult.Lockout;
            }
            string? codeInServer = await _sysUserRepository.RetrievePhoneCodeAsync(phoneNumberVo);
            if (string.IsNullOrEmpty(codeInServer))
            {
                return EnumCheckCodeResult.OK;
            }
            else
            {
                LoginFail(sysAdminEntity);
                return EnumCheckCodeResult.CodeError;
            }
        }
        #endregion
    }

    public enum EnumCheckCodeResult
    {
        OK,
        Lockout,
        CodeError,
        PhoneNumberNotFound,
    }
}
